package org.example.controller;

import org.apache.ibatis.annotations.Delete;
import org.example.domain.JsonData;
import org.example.domain.Meta;
import org.example.domain.PageBean;
import org.example.domain.Role;
import org.example.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Jimmy
 * @version 1.0
 */
@Controller
@ResponseBody
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 分页查询
     * @param pagenum 当前页码
     * @param pagesize 每页条目数
     * @return json
     */
    @GetMapping(value = "/roles")
    public JsonData<PageBean<Role>> pageBean(int pagenum,int pagesize){
        Meta meta;
        PageBean<Role> pageBean = roleService.findByPage(pagenum, pagesize);
        if (pageBean != null){
            meta = new Meta("获取成功",200);
        }else {
            meta = new Meta("获取失败",404);
        }

        return new JsonData<>(pageBean,meta);
    }

    /**
     * 添加角色信息
     * @param role role
     * @return json
     */
    @PostMapping(value = "addRole")
    public JsonData<Role> addRole(@RequestBody Role role){
        Meta meta;
        int i = roleService.addRole(role);
        if (i != 0){
            meta = new Meta("添加成功",200);
            return new JsonData<>(role,meta);
        }else {
            meta = new Meta("添加失败",500);
            return new JsonData<>(meta);
        }
    }

    /**
     * 根据id获取角色信息
     * @param rid 角色id
     * @return json
     */
    @GetMapping(value = "/getRole/{rid}")
    public JsonData<Role> findById(@PathVariable(value = "rid") String rid){
        Meta meta;
        Role role = roleService.findOneById(rid);
        if (role != null){
            meta = new Meta("获取成功",200);
        }else {
            meta = new Meta("获取失败",404);
        }
        return new JsonData<>(role,meta);
    }

    /**
     * 修改角色基本信息
     * @param rid 角色id
     * @param role role
     * @return json
     */
    @PutMapping(value = "/updateRole/{rid}")
    public JsonData<String> update(@PathVariable(value = "rid")String rid,@RequestBody(required = false)Role role){
        Meta meta;
        role.setRid(rid);
        int i = roleService.update(role);
        if (i != 0){
            meta = new Meta("修改成功",200);
        }else {
            meta = new Meta("修改失败",404);
        }
        return new JsonData<>(meta);
    }

    /**
     * 删除
     * @param rid 角色id
     * @return json
     */
    @Delete(value = "/deleteRole/{rid}")
    public JsonData<String> delete(@PathVariable(value = "rid")String rid){
        Meta meta;
        int i = roleService.delete(rid);
        if (i != 0){
            meta = new Meta("删除成功",200);
        }else {
            meta = new Meta("删除失败",404);
        }
        return new JsonData<>(meta);
    }

    /**
     * 角色授权
     * @param rid 角色id
     * @param modelIds 模块id数组
     * @return json
     */
    @PostMapping(value = "/addRolePower/{rid}")
    public JsonData<String> addRolePower(@PathVariable(value = "rid")String rid, Map<String,List<String>> modelIds){
        Meta meta;
        roleService.addRolePower(rid,modelIds.get("modelIds"));
        meta = new Meta("授权成功",200);
        return new JsonData<>(meta);
    }

    /**
     * 删除角色权限
     * @param rid 角色id
     * @param modelId 模块id
     * @return json
     */
    @Delete(value = "/deleteRolePower/{rid}/{modelId}")
    public JsonData<List<Role>> deleteRolePower(@PathVariable(value = "rid")String rid,@PathVariable(value = "modelId")String modelId){
        Meta meta;
        List<Role> roles = null;
        int i = roleService.deleteRolePower(rid, modelId);
        if (i != 0){
            meta = new Meta("删除成功",200);
            roles = roleService.findByPageNoPage();
        }else {
            meta = new Meta("删除失败",404);
        }
        return new JsonData<>(roles,meta);
    }


}
